package com.lonwin.mes2u8.service;

import com.alibaba.fastjson.JSONObject;
import com.lonwin.mes2u8.common.CommonValue;
import com.lonwin.mes2u8.common.YonYouConfig;
import com.lonwin.mes2u8.domain.u8.U8CommonSyncResponse;
import com.lonwin.mes2u8.domain.u8.U8CustomerAddParam;
import com.lonwin.mes2u8.domain.u8.U8CustomerQueryResult;
import com.lonwin.mes2u8.domain.u8.U8CustomerQueryResultInfo;
import lombok.extern.slf4j.Slf4j;
import openapi4j.exception.OpenAPIException;
import openapi4j.service.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 客户相关操作
 * @author DELL
 * @Date 2023-12-29 14:06
 */
@Service
@Slf4j
public class CustomerAppService {
    @Resource
    private YonYouConfig yonYouConfig;

    /**
     * 根据客户id查询客户信息
     * @param customerId 客户id
     * @return 客户信息
     */
    public U8CustomerQueryResultInfo queryCustomerFromU8ById(String customerId) {
        CustomerService customerService = new CustomerService();
        JSONObject customerJson = null;
        try {
            customerJson = customerService.get(customerId, yonYouConfig.getToAccount());
        } catch (OpenAPIException e) {
            log.error("CustomerAppService.queryCustomerFromU8ById occur exception by sdk : {}", e.getMessage());
            return null;
        }

        if (Objects.nonNull(customerJson)) {
            U8CustomerQueryResult u8CustomerQueryResult = JSONObject.parseObject(JSONObject.toJSONString(customerJson), U8CustomerQueryResult.class);
            return CommonValue.U8_RESPONSE_SUCCESS.equals(u8CustomerQueryResult.getErrcode()) ? u8CustomerQueryResult.getCustomer() : null;
        }

        return null;

    }

    /**
     * 判断客户是否存在于u8中
     * @param customerId 客户编码
     * @return 判断结果，不存在为true
     */
    public boolean isNotExistU8(String customerId) {
        return Objects.isNull(queryCustomerFromU8ById(customerId));
    }


    /**
     * 向U8中新增客户
     * @param u8CustomerAddParam 新增客户参数对象
     * @return 新增结果
     */
    public U8CommonSyncResponse addCustomerToU8(U8CustomerAddParam u8CustomerAddParam) {
        CustomerService customerService = new CustomerService();
        JSONObject addJson = null;
        try {
            addJson = customerService.add(JSONObject.toJSONString(u8CustomerAddParam), yonYouConfig.getToAccount());
        } catch (OpenAPIException e) {
            log.error("CustomerAppService.addCustomerToU8 occur exception by sdk : {}", e.getMessage());
            return null;
        }

        return Objects.nonNull(addJson) ? JSONObject.parseObject(JSONObject.toJSONString(addJson), U8CommonSyncResponse.class) : null;
    }

    /**
     * 如果客户在U8中不存在，则新增
     * @param u8CustomerAddParam
     * @return
     */
    public U8CommonSyncResponse addCustomerToU8IfNotExist(U8CustomerAddParam u8CustomerAddParam) {
        if(isNotExistU8(u8CustomerAddParam.getCustomer().getCode())) {
            U8CommonSyncResponse u8CommonSyncResponse = addCustomerToU8(u8CustomerAddParam);
            return u8CommonSyncResponse;
        }
        return null;
    }
}
